﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Antioch.Base.PlayerStates
{
    internal class ClimbOntoLedge : IState
    {
        int ClimbTimer;
        Vector2 LedgeOwnerLastPosition;
        Vector2 Gravity;

        enum ClimbState
        {
            Up,
            Over,
            CoolDown
        }

        private ClimbState State;

        public void Enter(StateMachine Machine)
        {
            Machine.DrawComponent.Play("ClimbLedge", false);
            Machine.PhysicsComponent.PhysicsBody.GetFixtureList().SetFriction(0.0f);

            ClimbTimer = 40;
            LedgeOwnerLastPosition = Machine.Memory.LedgeHangPoint.Me.Position;
            Gravity = Jemgine.Math.Vector.Normalize(Machine.PhysicsComponent.Gravity);

            Machine.PhysicsComponent.SetGravity(Vector2.Zero);
            Machine.PhysicsComponent.AffectedByGravity = false;

            State = ClimbState.Up;
        }

        public void Leave(StateMachine Machine)
        {
            Machine.PhysicsComponent.AffectedByGravity = true;
        }

        public void Update(StateMachine Machine)
        {
            --ClimbTimer;
            if (ClimbTimer < 0) 
            {
                Machine.Transition(typeof(Stand));
                return;
            }

            Vector2 LedgeOwnerDelta = Machine.Memory.LedgeHangPoint.Me.Position - LedgeOwnerLastPosition;
            Machine.PhysicsComponent.PhysicsBody.Position += LedgeOwnerDelta;
            LedgeOwnerLastPosition = Machine.Memory.LedgeHangPoint.Me.Position;

            if (!Machine.Memory.LedgeHangPoint.CanHang(Machine.PlayerEntity))
            {
                Machine.Transition(typeof(Stand));
                return;
            }

            Vector2 LedgePosition = Machine.Memory.LedgeHangPoint.Me.TransformToWorldSpace(
                Machine.Memory.LedgeHangPoint.Position);
            LedgePosition = Machine.PlayerEntity.TransformToLocalSpace(LedgePosition);

            switch (State)
            {
                case ClimbState.Up :
                    {
                        if (LedgePosition.Y < -Machine.PhysicsComponent.Dimensions.Y / 2)
                        {
                            State = ClimbState.Over;
                            ClimbTimer = 12;
                        }
                        else
                            Machine.PhysicsComponent.PhysicsBody.Position += (-Gravity) * (8.0f / 20.0f);
                    }
                    break;
                case ClimbState.Over :
                    {
                        Vector2 Left = Jemgine.Math.Vector.Perpendicular(Gravity);
                        if (Machine.PlayerEntity.Facing == Jemgine.Data.Facings.Right) Left *= -1;
                        Machine.PhysicsComponent.PhysicsBody.Position += Left * (1.5f / 10.0f);

                        if (ClimbTimer == 2) State = ClimbState.CoolDown;
                    }
                    break;
                case ClimbState.CoolDown :
                    break;
            }

        }
    }
}
